package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSON;
import com.bdqn.tour.pojo.HzyLocation;
import com.bdqn.tour.pojo.HzyPageInfo;
import com.bdqn.tour.service.impl.HzyLocationServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/locationInfo")
public class LocationInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        // 获取分页参数
        int pageNo = 1;
        int pageSize = 5;
        if (req.getParameter("pageNo")!=null){
            pageNo = Integer.parseInt(req.getParameter("pageNo"));
        }
        if (req.getParameter("pageSize")!=null){
            pageSize = Integer.parseInt(req.getParameter("pageSize"));
        }

        //获取分页数据
        HzyPageInfo<HzyLocation> pageInfo = new HzyLocationServiceImpl().getLocationPage(pageNo,pageSize);

        // 创建响应对象
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", pageInfo);
        response.put("message", "获取数据成功");

        PrintWriter out = resp.getWriter();
        String json = JSON.toJSONString(response);

        out.print(json);
        out.flush();
        out.close();
    }
}
